package org.dfl.plugin.editor.parts;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import org.dfl.messages.Messages;
import org.dfl.plugin.editor.commands.ConnectionDeleteCommand;
import org.dfl.plugin.editor.figures.ConnectionFigure;
import org.dfl.plugin.editor.model.ConnectionModelElement;
import org.dfl.plugin.editor.model.ModelElement;
import org.eclipse.draw2d.Graphics;
import org.eclipse.draw2d.IFigure;
import org.eclipse.gef.EditPolicy;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.editparts.AbstractConnectionEditPart;
import org.eclipse.gef.editpolicies.ConnectionEditPolicy;
import org.eclipse.gef.editpolicies.ConnectionEndpointEditPolicy;
import org.eclipse.gef.requests.GroupRequest;


/**
 * Edit part for Connection model elements.
 * <p>
 * This edit part must implement the PropertyChangeListener interface, so it can
 * be notified of property changes in the corresponding model element.
 * </p>
 * 
 * @author Piotr Wlodarczyk
 */
class ConnectionEditPart extends AbstractConnectionEditPart implements
		PropertyChangeListener {

	
	/**
	 * Upon activation, attach to the model element as a property change
	 * listener.
	 */
	public void activate() {
		if (!isActive()) {
			super.activate();
			((ModelElement) getModel()).addPropertyChangeListener(this);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractEditPart#createEditPolicies()
	 */
	protected void createEditPolicies() {
		// Selection handle edit policy.
		// Makes the connection show a feedback, when selected by the user.
		installEditPolicy(EditPolicy.CONNECTION_ENDPOINTS_ROLE,
				new ConnectionEndpointEditPolicy());

		// Allows the removal of the connection model element
		installEditPolicy(EditPolicy.CONNECTION_ROLE,
				new ConnectionEditPolicy() {
					protected Command getDeleteCommand(GroupRequest request) {
						return new ConnectionDeleteCommand(getCastedModel());
					}
				});
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see org.eclipse.gef.editparts.AbstractGraphicalEditPart#createFigure()
	 */
	protected IFigure createFigure() {
		ConnectionFigure f = new ConnectionFigure();
		refreshFigure(f);
		return f;

	}
	
	protected void refreshFigure(IFigure ff) {
		ConnectionFigure f = (ConnectionFigure) ff;
		f.setLineStyle(Graphics.LINE_SOLID);
		f.setInputLabel(getCastedModel().getInputName());
		
		String propertiesLabel = Messages.ConnectionEditPart_0;
		if(getCastedModel().getEdge().isNested()){
			propertiesLabel+=Messages.ConnectionEditPart_xLabel;
			f.setLineStyle(Graphics.LINE_DOT);
		}
		if(getCastedModel().getEdge().isNeededEmpty()){
			propertiesLabel+=Messages.ConnectionEditPart_emptyLabel;
		}
		if(getCastedModel().getEdge().isNeededNotempty()){
			propertiesLabel+=Messages.ConnectionEditPart_notEmptyLabel;
		}
		if(getCastedModel().getEdge().isNeededTrue()){
			propertiesLabel+=Messages.ConnectionEditPart_trueLabel;
		}
		if(getCastedModel().getEdge().isNeededFalse()){
			propertiesLabel+=Messages.ConnectionEditPart_falseLabel;
		}
		f.setPropertiesLabel(propertiesLabel);
	}
	

	/**
	 * Upon deactivation, detach from the model element as a property change
	 * listener.
	 */
	public void deactivate() {
		if (isActive()) {
			super.deactivate();
			((ModelElement) getModel()).removePropertyChangeListener(this);
		}
	}

	private ConnectionModelElement getCastedModel() {
		return (ConnectionModelElement) getModel();
	}

	
	/*
	 * (non-Javadoc)
	 * 
	 * @see java.beans.PropertyChangeListener#propertyChange(java.beans.PropertyChangeEvent)
	 */
	public void propertyChange(PropertyChangeEvent evt) {
		String prop = evt.getPropertyName();
		if (ConnectionModelElement.SELF_PROP.equals(prop)) {
			refreshFigure(getFigure());
			refreshVisuals();
		}
	}
}